package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TestPlan;
import com.ruoyi.system.mapper.TestItemMapper;
import com.ruoyi.system.domain.TestItem;
import com.ruoyi.system.service.ITestItemService;
import com.ruoyi.common.core.text.Convert;

/**
 * test_itemService业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-27
 */
@Service
public class TestItemServiceImpl implements ITestItemService 
{
    @Autowired
    private TestItemMapper testItemMapper;

    /**
     * 查询test_item
     * 
     * @param itemId test_item主键
     * @return test_item
     */
    @Override
    public TestItem selectTestItemByItemId(Long itemId)
    {
        return testItemMapper.selectTestItemByItemId(itemId);
    }

    /**
     * 查询test_item列表
     * 
     * @param testItem test_item
     * @return test_item
     */
    @Override
    public List<TestItem> selectTestItemList(TestItem testItem)
    {
//        return testItemMapper.selectTestItemList(testItem);
        List<TestItem> testItemList = testItemMapper.selectTestItemList(testItem);
        for (int i = 0; i < testItemList.size(); i++) {
            Long userId = testItemList.get(i).getUserId();
            SysUser user = testItemMapper.selectUserByUserId(userId);
            testItemList.get(i).setUserName(user.getUserName());
        }
        return testItemList;
    }

    /**
     * 新增test_item
     * 
     * @param testItem test_item
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTestItem(TestItem testItem)
    {
        int rows = testItemMapper.insertTestItem(testItem);
        insertTestPlan(testItem);
        return rows;
    }

    /**
     * 修改test_item
     * 
     * @param testItem test_item
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTestItem(TestItem testItem)
    {
        testItemMapper.deleteTestPlanByPlanId(testItem.getItemId());
        insertTestPlan(testItem);
        return testItemMapper.updateTestItem(testItem);
    }

    /**
     * 批量删除test_item
     * 
     * @param itemIds 需要删除的test_item主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestItemByItemIds(String itemIds)
    {
        testItemMapper.deleteTestPlanByPlanIds(Convert.toStrArray(itemIds));
        return testItemMapper.deleteTestItemByItemIds(Convert.toStrArray(itemIds));
    }

    /**
     * 删除test_item信息
     * 
     * @param itemId test_item主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestItemByItemId(Long itemId)
    {
        testItemMapper.deleteTestPlanByPlanId(itemId);
        return testItemMapper.deleteTestItemByItemId(itemId);
    }

    /**
     * 新增test_plan信息
     * 
     * @param testItem test_item对象
     */
    public void insertTestPlan(TestItem testItem)
    {
        List<TestPlan> testPlanList = testItem.getTestPlanList();
        Long itemId = testItem.getItemId();
        if (StringUtils.isNotNull(testPlanList))
        {
            List<TestPlan> list = new ArrayList<TestPlan>();
            for (TestPlan testPlan : testPlanList)
            {
                testPlan.setPlanId(itemId);
                list.add(testPlan);
            }
            if (list.size() > 0)
            {
                testItemMapper.batchTestPlan(list);
            }
        }
    }

    /**
     * 查询方法，这里可以到时候做数据验证，避免重复数据
     *
     */
    @Override
    public List<SysUser> selectUserName() {
        return testItemMapper.selectUserName();
    }
}
